{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  汪虎晨\n",
    "+ 学号  0165212"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沙海行旱舟，冰山做酒壶\n",
      "二十多年没朋友，天涯任我游\n",
      "孑然一身勇，算什么英雄\n",
      "世间太多伤心愁 我身后三只狗\n",
      "大的叫孤勇，小的叫词穷\n",
      "不大不小的最没用，名字叫踟蹰\n",
      "月上柳梢头，相约去青楼\n",
      "怒发冲冠凭栏处我身边一壶酒\n",
      "醉眼看人间，个个都温柔\n",
      "杯中尽是侠客冢，我还不想\n",
      "也有人吟阙，也有人歌舞\n",
      "一腔诗意喂了狗，我也不愿回头\n",
      "一句似一箭，万箭读穿心\n",
      "白驹一晃人已瘦，少年化老朽\n",
      "正看尘满面，侧望腰已偻\n",
      "你说不如打个赌，输了不许走\n",
      "醉眼看人间，个个都温柔\n",
      "杯中尽是侠客冢，我还不想\n",
      "也有人吟阙，也有人歌舞\n",
      "一腔诗意喂了狗，我也不愿回头\n"
     ]
    }
   ],
   "source": [
    "def lyric(lrc):\n",
    "    print('沙海行旱舟，冰山做酒壶\\n二十多年没朋友，天涯任我游\\n孑然一身勇，算什么英雄\\n世间太多伤心愁 我身后三只狗\\n大的叫孤勇，小的叫词穷\\n不大不小的最没用，名字叫踟蹰\\n月上柳梢头，相约去青楼\\n怒发冲冠凭栏处我身边一壶酒\\n%s'%lrc)\n",
    "    print('一句似一箭，万箭读穿心\\n白驹一晃人已瘦，少年化老朽\\n正看尘满面，侧望腰已偻\\n你说不如打个赌，输了不许走\\n%s'%lrc)\n",
    "lyric('醉眼看人间，个个都温柔\\n杯中尽是侠客冢，我还不想\\n也有人吟阙，也有人歌舞\\n一腔诗意喂了狗，我也不愿回头')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "均值为2.00\n",
      "方差为0.00\n",
      "n为15\n",
      "\n",
      "均值为36.29\n",
      "方差为973.92\n",
      "n为14\n"
     ]
    }
   ],
   "source": [
    "def number(*args):\n",
    "    sum = 0\n",
    "    n = 0\n",
    "    var = 0\n",
    "    for value in args:\n",
    "        sum = sum+value\n",
    "        n = n+1\n",
    "    average = sum/n\n",
    "    for value in args:\n",
    "        var = var+(value-average)**2\n",
    "    var = var/n\n",
    "    print('\\n均值为%.2f'%average)\n",
    "    print('方差为%.2f'%var)\n",
    "    print('n为%s'%n)\n",
    "number(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)\n",
    "number(2,3,4,5,6,12,45,63,78,95,23,56,45,71)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "输出样本标准差请输入1，输出方差请输入2\n",
      "1\n",
      "均值为2.00\n",
      "n为15\n",
      "标准差为0.00\n",
      "\n",
      "输出样本标准差请输入1，输出方差请输入2\n",
      "2\n",
      "均值为36.29\n",
      "n为14\n",
      "方差为973.92\n"
     ]
    }
   ],
   "source": [
    "def number(*args,feature=None):\n",
    "    print('\\n输出样本标准差请输入1，输出方差请输入2')\n",
    "    feature = int(input())\n",
    "    sum = 0\n",
    "    n = 0\n",
    "    var = 0\n",
    "    for value in args:\n",
    "        sum = sum+value\n",
    "        n = n+1\n",
    "    average = sum/n\n",
    "    for value in args:\n",
    "        var = var+(value-average)**2\n",
    "    var = var/n\n",
    "    stdev = var**(1/2)\n",
    "    print('均值为%.2f'%average)\n",
    "    print('n为%s'%n)\n",
    "    if feature == 1:\n",
    "        print('标准差为%.2f'%stdev)\n",
    "    if feature == 2:\n",
    "        print('方差为%.2f'%var)\n",
    "number(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)\n",
    "number(2,3,4,5,6,12,45,63,78,95,23,56,45,71)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "输出均值的标准误、最大值、最小值请输入1，不输出请输入2\n",
      "2\n",
      "均值为2.00\n",
      "n为15\n",
      "\n",
      "输出均值的标准误、最大值、最小值请输入1，不输出请输入2\n",
      "1\n",
      "均值为36.29\n",
      "n为14\n",
      "标准误为31.21\n",
      "最大值为95\n",
      "最小值为2\n"
     ]
    }
   ],
   "source": [
    "def number(*args,feature=None,SE=None,max=None,min=None):\n",
    "    print('\\n输出均值的标准误、最大值、最小值请输入1，不输出请输入2')\n",
    "    feature = int(input())\n",
    "    sum = 0\n",
    "    n = 0\n",
    "    var = 0\n",
    "    for value in args:\n",
    "        sum = sum+value\n",
    "        n = n+1\n",
    "        max = value\n",
    "        min = value\n",
    "    for value in args:\n",
    "        if value>max:\n",
    "            max = value\n",
    "        if value<min:\n",
    "            min = value\n",
    "    average = sum/n\n",
    "    for value in args:\n",
    "        var = var+(value-average)**2\n",
    "    var = var/n\n",
    "    SE = var**(1/2)\n",
    "    print('均值为%.2f'%average)\n",
    "    print('n为%s'%n)\n",
    "    if feature == 1:\n",
    "        print('标准误为%.2f'%SE+'\\n最大值为%s'%max+'\\n最小值为%s'%min)\n",
    "number(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)\n",
    "number(2,3,4,5,6,12,45,63,78,95,23,56,45,71)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
